package io.github.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.AnswerGroupDao;
import io.github.dao.ExtensionDao;
import io.github.entity.AnswerGroupEntity;
import io.github.service.AnswerGroupService;
import io.github.service.ExtensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Max
 * <br>
 * date 2019/12/28 10:24
 */

@Service
public class AnswerGroupServiceImpl extends ServiceImpl<AnswerGroupDao, AnswerGroupEntity> implements AnswerGroupService {


    @Autowired
    ExtensionService extensionService;

    @Override
    public void save(AnswerGroupEntity entity) {
        baseMapper.insert(entity);
    }

    @Override
    public void delete(Long id) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("answer_group",id);
        int count = extensionService.selectCount(wrapper);
        if(count>0) {
            throw new IllegalArgumentException();
        }else {
            this.baseMapper.deleteById(id);
        }
    }


    @Override
    public AnswerGroupEntity getByName(String name){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("name",name);
        return selectOne(wrapper);
    }

    @Override
    public List<AnswerGroupEntity> list() {
        return baseMapper.selectList(new EntityWrapper<>());
    }

    @Override
    public AnswerGroupEntity getById(Long id) {
        return baseMapper.selectById(id);
    }
}